import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_changdian_football/pages/clubs/clubs_old_page.dart';
import 'package:flutter_changdian_football/pages/clubs/clubs_page.dart';
import 'package:flutter_changdian_football/pages/home/home_page.dart';
import 'package:flutter_changdian_football/pages/main/main_controller.dart';
import 'package:flutter_changdian_football/pages/match/match_page.dart';
import 'package:flutter_changdian_football/pages/mine/mine_page.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:flutter_changdian_football/widget/toast/custom_toast.dart';
import 'package:get/get_state_manager/get_state_manager.dart';

class MainPage extends StatelessWidget {
  const MainPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MainController>(
      init: MainController(),
      builder: (mainController) {
        return WillPopScope(
          child: Scaffold(
            resizeToAvoidBottomInset: false,
            body: PageView(
              physics: const NeverScrollableScrollPhysics(),
              controller: mainController.pageController,
              onPageChanged: mainController.changePage,
              children: const [
                HomePage(),

                MatchPage(),

                ClubsPage(),

                MinePage(),
              ],
            ),
            bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              currentIndex: mainController.currentPage,
              onTap: mainController.jumpToPage,
              selectedFontSize: 10.sp,
              unselectedFontSize: 10.sp,
              selectedItemColor: const Color(0xff499265),
              unselectedItemColor: const Color(0xff242941),
              elevation: 0.0,
              mouseCursor: MouseCursor.uncontrolled,
              backgroundColor: Colors.white,
              unselectedLabelStyle: TextStyle(fontWeight: FontWeight.bold, fontSize: 10.sp, ),
              selectedLabelStyle: TextStyle(fontWeight: FontWeight.bold, fontSize: 10.sp, ),
              items: [
                BottomNavigationBarItem(
                  icon: _bottomNavigationImg('assets/images/icon_tab_home.png'),
                  activeIcon: _bottomNavigationImg('assets/images/icon_tab_home_pre.png'),
                  label: '首页',
                  tooltip: ""
                ),

                
                BottomNavigationBarItem(
                  icon: _bottomNavigationImg('assets/images/icon_tab_bs.png'),
                  activeIcon: _bottomNavigationImg('assets/images/icon_tab_bisai.png'),
                  label: '比赛',
                  tooltip: ""
                ),
                BottomNavigationBarItem(
                  icon: _bottomNavigationImg('assets/images/icon_tab_jlb.png'),
                  activeIcon: _bottomNavigationImg('assets/images/icon_tab_jlb_pre.png'),
                  label: '俱乐部',
                  tooltip: ""
                ),
                BottomNavigationBarItem(
                  icon: _bottomNavigationImg('assets/images/icon_tab_me.png'),
                  activeIcon: _bottomNavigationImg('assets/images/icon_tab_me_pre.png'),
                  label: '我的',
                  tooltip: ""
                ),
              ]
            ),
          ), 
          onWillPop: () async {
            int newTime = DateTime.now().millisecondsSinceEpoch;
            int result = newTime - mainController.lastTime;
            mainController.lastTime = newTime;
            if (result > 2000) {
              CustomToast.showTextToast("再按一次退出");
            } else {
              SystemNavigator.pop();
            }
            return false;
          },
        );
      }
    );
  }

  _bottomNavigationImg(String img) {
    return Container(
      margin: EdgeInsets.only(bottom: 2.w),
      child: Image.asset(
        img,
        width: 20.w,
        height: 20.w,
        excludeFromSemantics: true,
        gaplessPlayback: true,
        fit: BoxFit.fill,
      ),
    );
  }

}